using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DetectLeaks : MonoBehaviour
{
    string textArea = "";
    Vector2 scrollPosition;

    void OnGUI()
    {
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("ShowDetails", GUILayout.Width(100)))
        {

            Object[] objects = FindObjectsOfType(typeof(UnityEngine.Object));

            Dictionary<string, int> dictionary = new Dictionary<string, int>();

            foreach (Object obj in objects)
            {
                string key = obj.GetType().ToString();
                if (dictionary.ContainsKey(key))
                {
                    dictionary[key]++;
                }
                else
                {
                    dictionary[key] = 1;
                }
            }

            List<KeyValuePair<string, int>> myList = new List<KeyValuePair<string, int>>(dictionary);
            myList.Sort(
            delegate(KeyValuePair<string, int> firstPair,
            KeyValuePair<string, int> nextPair)
            {
                return nextPair.Value.CompareTo((firstPair.Value));
            }
            );

            textArea = "";

            foreach (KeyValuePair<string, int> entry in myList)
            {
                //GUILayout.Label (entry.Key + ": " + entry.Value);
                textArea += entry.Key + ": " + entry.Value + "\n";
            }
        }

        if (GUILayout.Button("HideDetails", GUILayout.Width(100)))
        {
            textArea = "";
        }
        GUILayout.EndHorizontal();

        if (textArea != "")
        {
            scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Width(300), GUILayout.Height(200));
            textArea = GUILayout.TextArea(textArea);
            GUILayout.EndScrollView();
        }

    }
}